package Demo;

public class Demo01 {
    private static int count = 0;
    private static Object locker = new Object();
    public static void main(String[] args) {
//        String s = "hello";
//        s = s + " world";  // 创建新对象，s引用指向新对象
//        System.out.println(s);

        Thread t1 = new Thread( () -> {
            for (int i = 0; i < 50000; i++) {
               synchronized (locker) {
                   count++;
               }
            }
        });
        Thread t2 = new Thread( () -> {
            for (int i = 0; i <50000; i++) {
                synchronized (locker) {
                    count++;
                }
            }
        });
        t1.start();
        t2.start();

        try {
            t1.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        try {
            t2.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(count);
    }
}
